package com.sitech.portal.server.dao.ibatis;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sitech.ibnms.core.BaseIbnmsDao;
import com.sitech.portal.server.dao.IMessageCenterDao;
import com.sitech.portal.server.domain.Message;
import com.sitech.portal.server.domain.MessageSetting;
import com.sitech.portal.server.form.MessageSettingForm;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;

public class MessageCenterDaoImpl extends BaseIbnmsDao implements IMessageCenterDao {
	/**
     * 插入（发送）一条消息
     * @param msg
     */
    public Message insertMessage(Message msg) {
        List mr = msg.getMessageReciever();
        PreparedStatement pstmt = null, pstmt1 = null, pstmt2 = null;
        ResultSet rs = null;
//            //生成消息主键
//            pstmt = this.prepareStatement("select SQ_SYS_MESSAGE.NEXTVAL MSG_ID from dual");
//            rs = pstmt.executeQuery();
//            rs.next();
//            msg.setMSG_ID(rs.getString(1));
//            String sql = "insert into tb_sys_message(MSG_ID,MSG_SENDER,MSG_CONTENT,MSG_SOURCE,MSG_TYPE,DETAILS_PATH,SEND_TIME) "
//                    + "values(?,?,?,?,?,?,sysdate)";
//            pstmt1 = this.getJdbcSqlMap().prepareStatement(sql);
//            // 插入消息主体
//            pstmt1.setString(1, msg.getMSG_ID());
//            pstmt1.setString(2, msg.getMSG_SENDER());
//            pstmt1.setString(3, msg.getMSG_CONTENT());
//            pstmt1.setString(4, msg.getMSG_SOURCE());
//            pstmt1.setString(5, msg.getMSG_TYPE());
//            pstmt1.setString(6, msg.getDETAILS_PATH());
//            pstmt1.executeUpdate();
//            // 插入消息与接收人的映射关系
//            String sql2 = "insert into tb_sys_message_user(MSG_ID,LOGIN_ID) values(?,?)";
//            pstmt2 = this.getJdbcSqlMap().prepareStatement(sql2);
//            if (mr != null) {
//                for (int i = 0; i < mr.size(); i++) {
//                    pstmt2.clearParameters();
//                    pstmt2.setString(1, msg.getMSG_ID());
//                    pstmt2.setString(2, mr.get(i) + "");
//                    pstmt2.executeUpdate();
//                }
//            }
        return msg;
    }

    /**
     * 阅读消息，调用此方法后表示login_id已经阅读了msg_id
     * @param msg_id
     * @param login_id
     */
    public void readMessage(String msg_id, String login_id) {
        Map map = new HashMap();
        map.put("MSG_ID", msg_id);
        map.put("LOGIN_ID", login_id);
        this.update("readMessage", map);
    }

    /**
     * 查找login_id的未读消息,READ_TIME=null
     * @param login_id
     * @return
     */
    public List<Message> findUnreadMessage(String login_id, String msg_id,MessageSetting setting) {
        HashMap param = new HashMap();
        param.put("LOGIN_ID", login_id);
        param.put("MSG_ID", msg_id);
        param.put("DISPLAY_SUM", setting.getDISPLAY_SUM());
        param.put("DISPLAY_SCOPE", setting.getDISPLAY_SCOPE());
        return this.query("findUnreadMessage", param);
    }

	@Override
	public List<Message> loadMsgByResponseId(String responseId) {
		// TODO Auto-generated method stub
		return this.query("loadMsgByResponseId",responseId);
	}

	@Override
	public void saveMsg(Message msg) {
		this.insert("saveMsg",msg);
	}

	@Override
	public void saveMsgUser(Message msg) {
		this.insert("insertMessageUser",msg);
	}

	@Override
	public List<Message> loadOnlineServerMsg(Message msg,MessageSetting setting) {
		/*HashMap param = new HashMap();
        param.put("LOGIN_ID", login_id);
        param.put("MSG_ID", msg_id);
        param.put("DISPLAY_SUM", setting.getDISPLAY_SUM());
        param.put("DISPLAY_SCOPE", setting.getDISPLAY_SCOPE());*/
		return this.query("loadOnlineServerMsg", msg);
	}

	@Override
	public List<TblSYS_LOGINMSG> loadOnlineMsgUser(String loginId,MessageSetting setting) {
		HashMap param = new HashMap();
        param.put("LOGIN_ID", loginId);
        //param.put("DISPLAY_SUM", setting.getDISPLAY_SUM());
        //param.put("DISPLAY_SCOPE", setting.getDISPLAY_SCOPE());
		return this.query("loadOnlineMsgUser", param);
	}

	@Override
	public boolean updateDisplaySum(Message message) {
		int sum = this.update("updateDisplaySum", message);
		if(sum == 1){
			return true;
		}else{
			return false;
		}
	}
}
